package dao;

import domain.Borrow;
import domain.BorrowApplication;
import domain.Equipment;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DruidUtil;

import java.sql.Date;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

/**
 * 简介说明: 借用数据
 *
 * @author: Half_Q_0115
 * @date: 2024/09/18 09:21:57
 * @version: 1.0
 */

public class BorrowDaoImpl {
    private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    public int addBorrowApplication(BorrowApplication ba) {
        String sql = "insert into borrow_application values(?,?,?,?,?,?)";
        ba.setApproval_status("等待审批");
        ba.setStart_date(Date.valueOf(LocalDate.now()));
        //System.out.println("数据库操作" + ba);
        Object[] param = {ba.getUser_id(), ba.getEquipment_id(), ba.getStart_date(), ba.getExpected_days(), ba.getApproval_status(), ba.getRemarks()};
        int result;
        try {
            result = qr.update(sql, param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public List<BorrowApplication> findMyBorrowApplication(String user_id) {
        //System.out.println("findMyBorrowApplication");
        String sql = "select equipment_id,start_date,expected_days,approval_status,remarks from borrow_application where user_id = ?";
        Object[] param = {user_id};
        List<BorrowApplication> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<BorrowApplication>(BorrowApplication.class), param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<BorrowApplication> findTeacherBorrowApplication(String user_id) {
        //System.out.println("findMyBorrowApplication");
        String sql = "SELECT ba.* FROM borrow_application ba JOIN equipment e ON ba.equipment_id = e.id WHERE e.staff_id = ?;";
        Object[] param = {user_id};
        List<BorrowApplication> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<BorrowApplication>(BorrowApplication.class), param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<BorrowApplication> findAllBorrowApplication() {
        //System.out.println("findMyBorrowApplication");
        String sql = "SELECT * FROM borrow_application";
        List<BorrowApplication> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<BorrowApplication>(BorrowApplication.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<Borrow> findMyBorrow(String user_id) {
        //System.out.println("findMyBorrowApplication");
        String sql = "select equipment_id,start_date,expected_days,remarks from borrow where user_id = ?";
        Object[] param = {user_id};
        List<Borrow> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<Borrow>(Borrow.class), param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<Borrow> findTeacherBorrow(String user_id) {
        //System.out.println("findTeacherBorrowApplication");
        String sql = "SELECT ba.* FROM borrow ba JOIN equipment e ON ba.equipment_id = e.id WHERE e.staff_id = ?;";
        Object[] param = {user_id};
        List<Borrow> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<Borrow>(Borrow.class), param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<Borrow> findAllBorrow() {
        //System.out.println("findTeacherBorrowApplication");
        String sql = "SELECT * FROM borrow ";
        List<Borrow> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<Borrow>(Borrow.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public boolean deleteBorrowApplication(String equipment_id, String user_id) {
        String sql = "delete from borrow_application where equipment_id = ? and user_id = ?";
        Object[] param = {equipment_id, user_id};
        int result;
        try {
            result = qr.update(sql, param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result > 0;
    }

    public BorrowApplication findBorrowApplicationByid(String equipment_id, String user_id) {
        String sql = "select * from borrow_application where equipment_id = ? and user_id = ?";
        Object[] param = {equipment_id, user_id};
        BorrowApplication ba = null;
        try {
            ba = qr.query(sql, new BeanHandler<BorrowApplication>(BorrowApplication.class), param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return ba;
    }

    public int updateApprovalStatus(String equipmentId, String status){
        String sql = "UPDATE borrow_application SET approval_status = ? WHERE equipment_id = ?";
        Object[] params = {status, equipmentId};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public int deleteBorrowById(String equipment_id)  {
        String sql = "DELETE FROM borrow WHERE equipment_id = ?";
        Object[] params = {equipment_id};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }


}
